//
//  KWTMGRNSettingEditTimeVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/2.
//

#import "KWTMGRNSettingEditTimeVC.h"
#import "SettingOptionController.h"
#import "WSDatePickerView.h"
#import "KWTTimePickerView.h"

@interface KWTMGRNSettingEditTimeVC ()<QMUITextFieldDelegate>
@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) QMUITextField *leftTextField;//开始时间

@property (nonatomic, strong) QMUITextField *rightTextField;//结束时间

@property (nonatomic, strong) QMUITextField *chgTextField;//充电/放电

@property (nonatomic, strong) QMUITextField *socTextField;//截止soc

@property (nonatomic, strong) QMUITextField *powerTextField;//充电/放电功率

@property (nonatomic, strong) QMUILabel *powerLab;
@property (nonatomic, strong) QMUILabel *socLab;
@end

@implementation KWTMGRNSettingEditTimeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCOLOR_HEX(@"#F0F5FF");
    [self setupUI];
    self.model = self.model;
}

-(void)setupUI
{
    kWeakSelf;
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    [sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [sureBtn setCornerRadius:8];
    [sureBtn setBackgroundColor:CMainColorGreen];
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(50);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom-10:-20);
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf sureAction];
    }];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.equalTo(sureBtn.mas_top).offset(-15);
    }];
    
    NSString *title = [NSString stringWithFormat:@"时段%ld设置".rh_localized,self.index];
    self.navigationItem.title = title.rh_localized;
    QMUILabel *titleLab = [QMUILabel labelWithText:title.rh_localized font:kSYSTEMFONT(16) textColor:kCOLOR_HEX_A(@"#171A1D", 1)];
    [bgView addSubview:titleLab];
    self.titleLab = titleLab;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(10);
        make.left.mas_offset(15);
    }];
    
    UIView *line = [[UIView alloc]init];
    [bgView addSubview:line];
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(1);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(10);
    }];
    
    
    QMUILabel *timeTitleLab = [QMUILabel labelWithText:@"时间设置".rh_localized font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8)];
    [bgView addSubview:timeTitleLab];
    [timeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(15);
        make.left.mas_offset(15);
    }];
    
    
    _leftTextField = [[QMUITextField alloc]init];
    _leftTextField.textAlignment = NSTextAlignmentCenter;
    _leftTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 15);
    _leftTextField.userInteractionEnabled = NO;
    _leftTextField.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    _leftTextField.layer.cornerRadius = 5;
    _leftTextField.layer.masksToBounds = YES;
    _leftTextField.font = kSYSTEMFONT(14);
    _leftTextField.placeholder = @"请选择".rh_localized;
    _leftTextField.textColor = kCOLOR_HEX(@"#171A1D");
    [bgView addSubview:_leftTextField];
    [_leftTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeTitleLab);
        make.height.offset(44);
        make.top.equalTo(timeTitleLab.mas_bottom).offset(10);
    }];
    
    QMUILabel *lineLab = [QMUILabel labelWithFont:kSYSTEMFONT(15) textColor:kCOLOR_HEX_A(@"#7E868E", 0.3)];
    lineLab.text = @"--";
    lineLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:lineLab];
    [lineLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.leftTextField.mas_right);
        make.centerY.equalTo(weakSelf.leftTextField);
        make.width.offset(60);
    }];
    
    _rightTextField = [[QMUITextField alloc]init];
    _rightTextField.textAlignment = NSTextAlignmentCenter;
    _rightTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 15);
    _rightTextField.userInteractionEnabled = NO;
    _rightTextField.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    _rightTextField.layer.cornerRadius = 5;
    _rightTextField.layer.masksToBounds = YES;
    _rightTextField.font = kSYSTEMFONT(14);
    _rightTextField.textColor = kCOLOR_HEX(@"#171A1D");
    _rightTextField.placeholder = @"请选择".rh_localized;
    [bgView addSubview:_rightTextField];
    [_rightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lineLab.mas_right);
        make.height.width.equalTo(weakSelf.leftTextField);
        make.centerY.equalTo(weakSelf.leftTextField);
        make.right.mas_offset(-15);
    }];
    
    
    QMUIButton *leftBtn = [[QMUIButton alloc]init];
    [bgView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.leftTextField);
    }];
    
    QMUIButton *rightBtn = [[QMUIButton alloc]init];
    [bgView addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.rightTextField);
    }];
    
    [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf selectChargeTime1:[weakSelf.model.items safeObjectAtIndex:0]];
    }];
    
    [[rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf selectChargeTime2:[weakSelf.model.items safeObjectAtIndex:1]];
    }];
    
    
    //----------------------------------------------------------------
    
    
    QMUILabel *chgLab = [QMUILabel labelWithText:@"充放电选择".rh_localized font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8)];
    [bgView addSubview:chgLab];
    [chgLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeTitleLab);
        make.top.equalTo(weakSelf.leftTextField.mas_bottom).offset(15);
    }];
    
    _chgTextField = [[QMUITextField alloc]init];
    _chgTextField.textAlignment = NSTextAlignmentLeft;
    _chgTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 15);
    _chgTextField.userInteractionEnabled = NO;
    _chgTextField.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    _chgTextField.layer.cornerRadius = 5;
    _chgTextField.layer.masksToBounds = YES;
    _chgTextField.font = kSYSTEMFONT(14);
    _chgTextField.textColor = kCOLOR_HEX(@"#171A1D");
    _chgTextField.placeholder = @"请选择".rh_localized;
    [bgView addSubview:_chgTextField];
    [_chgTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chgLab);
        make.height.equalTo(weakSelf.leftTextField);
        make.top.equalTo(chgLab.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.rightTextField);
    }];
    
    UIImageView *arrow_chg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_2"]];
    [bgView addSubview:arrow_chg];
    [arrow_chg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(14);
        make.centerY.equalTo(weakSelf.chgTextField);
        make.right.equalTo(weakSelf.chgTextField).offset(-5);
    }];
    
    QMUIButton *chgBtn = [[QMUIButton alloc]init];
    [bgView addSubview:chgBtn];
    [chgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.chgTextField);
    }];
    
   
    //充放电选择
    [[chgBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf selectModelAction:[weakSelf.model.items safeObjectAtIndex:2]];
    }];
   
    //----------------------------------------------------------------
    QMUILabel *socTitleLab = [QMUILabel labelWithText:@"充电截止SOC (%)".rh_localized font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8)];
    [bgView addSubview:socTitleLab];
    self.socLab = socTitleLab;
    [socTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chgLab);
        make.top.equalTo(weakSelf.chgTextField.mas_bottom).offset(15);
    }];
    
    _socTextField = [[QMUITextField alloc]init];
    _socTextField.textAlignment = NSTextAlignmentLeft;
    _socTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 15);
    _socTextField.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    _socTextField.layer.cornerRadius = 5;
    _socTextField.layer.masksToBounds = YES;
    _socTextField.font = kSYSTEMFONT(14);
    _socTextField.textColor = kCOLOR_HEX(@"#171A1D");
    _socTextField.placeholder = @"请输入".rh_localized;
    _socTextField.delegate = self;
    _socTextField.maximumTextLength = 3;
    [bgView addSubview:_socTextField];
    [_socTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.leftTextField);
        make.height.equalTo(weakSelf.leftTextField);
        make.top.equalTo(socTitleLab.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.rightTextField);
    }];
    
    
    //----------------------------------------------------------------
    _powerLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@(W)",@"放电功率".rh_localized] font:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8)];
    [bgView addSubview:_powerLab];
    [_powerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(chgLab);
        make.top.equalTo(weakSelf.socTextField.mas_bottom).offset(15);
       
    }];
    
   
    _powerTextField = [[QMUITextField alloc]init];
    _powerTextField.textAlignment = NSTextAlignmentLeft;
    _powerTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 15);
    _powerTextField.backgroundColor = kCOLOR_HEX(@"#F3F7FF");
    _powerTextField.layer.cornerRadius = 5;
    _powerTextField.layer.masksToBounds = YES;
    _powerTextField.font = kSYSTEMFONT(14);
    _powerTextField.placeholder = @"请输入".rh_localized;
    _powerTextField.delegate = self;
    _powerTextField.maximumTextLength = 5;
    [bgView addSubview:_powerTextField];
    [_powerTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.leftTextField);
        make.height.equalTo(weakSelf.leftTextField);
        make.top.equalTo(weakSelf.powerLab.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.rightTextField);
    }];
    
   
}

-(void)setModel:(KWTMGRNSettingModel *)model
{
    _model = model;
    //开始时间
    KWTMGRNSettingModel *beginTimeModel = [model.items safeObjectAtIndex:0];
    self.leftTextField.text = beginTimeModel.content;
    
    //结束时间
    KWTMGRNSettingModel *endTimeModel = [model.items safeObjectAtIndex:1];
     self.rightTextField.text = endTimeModel.content;
    
    //充放电选择
    KWTMGRNSettingModel *chgEnableModel = [model.items safeObjectAtIndex:2];
     self.chgTextField.text = chgEnableModel.enumText.rh_localized;
    
    //截止SOC
    KWTMGRNSettingModel *socModel = [model.items safeObjectAtIndex:3];
     self.socTextField.text = socModel.content;
    
    //放电功率
    KWTMGRNSettingModel *powerModel = [model.items safeObjectAtIndex:4];
     self.powerTextField.text = powerModel.content;
    
    BOOL isCharge = [chgEnableModel.content integerValue] == 1;
    self.socLab.text = isCharge?@"充电截止SOC (%)".rh_localized:@"放电截止SOC (%)".rh_localized;
    self.powerLab.text = isCharge?[NSString stringWithFormat:@"%@(W)",@"充电功率".rh_localized]:[NSString stringWithFormat:@"%@(W)",@"放电功率".rh_localized];
    
   
    //静置
    if ([chgEnableModel.content integerValue] == 2) {
        self.socTextField.enabled = NO;
        self.powerTextField.enabled = NO;
//        self.chgTextField.text = @"静置".rh_localized;
    }else{
        self.socTextField.enabled = YES;
        self.powerTextField.enabled = YES;
    }
    
}


#pragma mark ------ 选择 充放电
-(void)selectModelAction:(KWTMGRNSettingModel*)model
{
    kWeakSelf;
    SettingOptionController *vc = [[SettingOptionController alloc] initWithData:model.selectList];
    [[RHWindowTipManage shareWindowTipManage] showWithController:vc];
    vc.curTextValue = [model.content integerValue];
    //如果是静置
    KWTMGRNSettingModel *socModel = [self.model.items safeObjectAtIndex:3];
    KWTMGRNSettingModel *powerModel = [self.model.items safeObjectAtIndex:4];
    if ([socModel.content integerValue]==0 && [powerModel.content integerValue]==0) {
        vc.curTextValue = 2;//静置
    }
    vc.selectBlock = ^(NSInteger index) {
        DevSettingOptionModel *select_model = model.selectList[index];
        model.enumText = select_model.text;//存要显示的内容
        model.content = [NSString stringWithFormat:@"%ld",select_model.textValue];//存下标
        
        //如果选择静置 soc,功率,充电使能,放电使能都设为0
        if([model.content integerValue] == 2 ){
            //截止SOC
            KWTMGRNSettingModel *socModel = [weakSelf.model.items safeObjectAtIndex:3];
            socModel.content = @"0";
            weakSelf.socTextField.text = socModel.content;
            weakSelf.socTextField.enabled = NO;
            //放电功率
            KWTMGRNSettingModel *powerModel = [weakSelf.model.items safeObjectAtIndex:4];
            powerModel.content = @"0";
            weakSelf.powerTextField.text = powerModel.content;
            weakSelf.powerTextField.enabled = NO;
            
//            //充电使能
//            KWTMGRNSettingModel *chgEnableModel = [self.model.items safeObjectAtIndex:2];
//            chgEnableModel.content = @"0";
//            self.chgTextField.text = chgEnableModel.enumText.rh_localized;
//
//            //放电使能
//            KWTMGRNSettingModel *GenModel = [self.model.items safeObjectAtIndex:5];
//            GenModel.content = @"0";
            
            
        }else{
            self.socTextField.enabled = YES;
            self.powerTextField.enabled = YES;
        }
        
        weakSelf.model = weakSelf.model;
        
    };
}


//充放电开始时间
-(void)selectChargeTime1:(KWTMGRNSettingModel*)timeModel
{
    kWeakSelf;
    KWTTimePickerView *pick = [[KWTTimePickerView alloc]initWithTitle:nil deviceType:@"" CompleteBlock:^(NSString * _Nonnull time) {
        timeModel.content = time;
        weakSelf.model = weakSelf.model;
    }];
    [pick setCurrentTime:timeModel.content];
    [pick show];
    return;
   
//    WSDatePickerView *datePickerV = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowHourMinute CompleteBlock:^(NSDate *selectDate) {
//        NSString *dateFormatStr = @"HH:mm";
//        NSString *currentDate = [selectDate stringWithFormat: dateFormatStr];
//        timeModel.content = currentDate;
//        weakSelf.model = weakSelf.model;
//    }];
//    datePickerV.dateLabelColor = FontColor(@"#3ECE80");//年-月 颜色
//    datePickerV.datePickerColor = UIColorBlack;//滚轮日期颜色
//    datePickerV.doneButtonColor = UIColor.blackColor;//确定按钮的颜色
//    [datePickerV show];
}

//充放电结束时间  必须大于或者等于开始时间
-(void)selectChargeTime2:(KWTMGRNSettingModel*)timeModel
{
    
    
    kWeakSelf;
    KWTTimePickerView *pick = [[KWTTimePickerView alloc]initWithTitle:nil deviceType:@"" CompleteBlock:^(NSString * _Nonnull time) {
        timeModel.content = time;
        weakSelf.model = weakSelf.model;
    }];
    KWTMGRNSettingModel *startTimeModel = [weakSelf.model.items firstObject];
    pick.startTime = startTimeModel.content;
    [pick setCurrentTime:timeModel.content];
    [pick show];
    
//    WSDatePickerView *datePickerV = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowHourMinute CompleteBlock:^(NSDate *selectDate) {
//        NSString *dateFormatStr = @"HH:mm";
//        NSString *currentDate = [selectDate stringWithFormat: dateFormatStr];
//        timeModel.content = currentDate;
//        weakSelf.model = weakSelf.model;
//    }];
//    datePickerV.dateLabelColor = FontColor(@"#3ECE80");//年-月 颜色
//    datePickerV.datePickerColor = UIColorBlack;//滚轮日期颜色
//    datePickerV.doneButtonColor = UIColor.blackColor;//确定按钮的颜色
//    [datePickerV show];
}


-(void)sureAction
{
    [self.view endEditing:YES];
    __block BOOL result = YES;
    [self.model.items enumerateObjectsUsingBlock:^(KWTMGRNSettingModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.content.length == 0 && ![obj.key containsString:@"Inv3_Gen"]) {//卖电使能不做判断
            result = NO;
            *stop = YES;
        }
    }];
    
    if (result == NO) {
        [QMUITips showInfo:@"参数不完整".rh_localized];
        return;
    }
    
    //结束时间要大于开始时间
    KWTMGRNSettingModel *startTimeModel = [self.model.items safeObjectAtIndex:0];
    KWTMGRNSettingModel *endTimeModel   = [self.model.items safeObjectAtIndex:1];
    NSString *startTime = [startTimeModel.content stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSString *endTime = [endTimeModel.content stringByReplacingOccurrencesOfString:@":" withString:@""];
    //可以设备开始时间和结束时间都是00:00
   
    if (([startTime integerValue] == 0) && ([endTime integerValue] == 0)) {
        
    }else if ([endTime integerValue] <= [startTime integerValue]) {
        [QMUITips showInfo:@"结束时间必须大于开始时间".rh_localized];
        return;
    }
    
    
    if (self.sureBlock) {
        self.sureBlock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}




-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //只能输入正数
    if ([string containsString:@"-"]) {
        return NO;
    }
    return [NSString checkIsIntOrDecimalNumber:range textFieldContent:textField.text inputString:string];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //截止soc  最大值100
    if ([textField isEqual:self.socTextField]) {
        KWTMGRNSettingModel *model = [self.model.items safeObjectAtIndex:3];
        model.content = [NSDecimalNumber decimalStringWithDouble:[textField.text doubleValue]];
        if ([model.content integerValue]>100) {
            model.content = @"100";
        }
    }
    
    //放电功率
    if ([textField isEqual:self.powerTextField]) {
        KWTMGRNSettingModel *model = [self.model.items safeObjectAtIndex:4];
        model.content = [NSDecimalNumber decimalStringWithDouble:[textField.text doubleValue]];
        if ([model.content integerValue]>12000) {
            model.content = @"12000";
        }
    }
    
    self.model = self.model;
}

@end
